//
//  ReleaseArticleController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/19.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ReleaseArticleController.h"

@interface ReleaseArticleController ()
/**标题*/
@property (nonatomic,strong)PubulicTextView*titleTexFiled;
/**内容*/
@property (nonatomic,strong)RZRichTextView*contentTexFiled;
/**标题站位文字*/
@property (nonatomic,strong)UILabel*titlePlaceholder;
/**内容站位文字*/
@property (nonatomic,strong)UILabel*contentPlaceholder;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*imageArray;
@end

@implementation ReleaseArticleController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavItems];
    [self createUI];
}
-(void)releseAction{
    NSMutableArray * images = [NSMutableArray array];
    if (self.contentTexFiled.rz_richTextImages.count > 0) {
        
        for (NSInteger i = 0 ; i<self.contentTexFiled.rz_richTextImages.count; i++) {
            NSString * name = self.imageArray[[Utilis getRandomNumber:0 to:14]];
            [images addObject:name];
        }
    }
    
    NSString * contenx = [self.contentTexFiled rz_codingToHtmlWithImageURLS:images];
    DLog(@"%@",contenx);
    //0-14
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:self.titleTexFiled.textView.text forKey:@"title"];
    [dic setValue:contenx forKey:@"contenx"];

    ShowActivityHud(@"发布中...");
    kWeakSelf(weakSelf);
    [PlistTool savePlistValue:dic Type:ReleseArticle Images:nil successful:^(NSString * _Nonnull message) {
        HideHubView;
        [weakSelf postNotification];
        [weakSelf backBtnClicked];
    }];

}

-(void)postNotification{
    [[NSNotificationCenter defaultCenter] postNotificationName:K_ReleaseArticle object:nil];
}
-(void)setNavItems{
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    leftBtn.titleLabel.font = SYSTEMFONT(16);
    [leftBtn sizeToFit];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"发布" forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SYSTEMFONT(16);
    [rightBtn sizeToFit];
    
    kWeakSelf(weakSelf);
    [rightBtn addActionHandler:^{
        if (self.titleTexFiled.textView.text.length == 0) {
            ShowHubMessage(@"请添加文章标题");
            return ;
        }
        if (self.contentTexFiled.text.length == 0) {
            ShowHubMessage(@"请添加文章正文");
            return ;
        }
        [weakSelf releseAction];
    }];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

-(void)createUI{
    
    UIView * oneline = UIView.new;
    oneline.backgroundColor = UIColorFromRGB(0xE9EEE7);
    
    UIView * twoline = UIView.new;
    twoline.backgroundColor = UIColorFromRGB(0xE9EEE7);

    
    [self.view addSubview:self.titleTexFiled];
    [self.view addSubview:self.contentTexFiled];
    [self.view addSubview:oneline];
    [self.view addSubview:twoline];

    [oneline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.titleTexFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(oneline.mas_bottom).offset(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    [twoline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.titleTexFiled.mas_bottom).offset(0);
        make.height.mas_equalTo(0.5);
    }];

    [self.contentTexFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(twoline.mas_bottom).offset(0);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(180);
    }];
    
    
    [self.titleTexFiled addSubview:self.titlePlaceholder];
    [self.contentTexFiled addSubview:self.contentPlaceholder];
  
    
    [self.titlePlaceholder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(6);
        make.top.mas_equalTo(10);
    }];
    [self.contentPlaceholder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(6);
        make.top.mas_equalTo(10);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTexView) name:UITextViewTextDidChangeNotification object:nil];

}
-(void)changeTexView{
    DLog(@"%@",self.titleTexFiled.textView.text);
    if ([self.titleTexFiled.textView.text isEqualToString:@""]) {
        self.titlePlaceholder.hidden = NO;
    }else{
        self.titlePlaceholder.hidden = YES;
    }
    if ([self.contentTexFiled.text isEqualToString:@""]) {
        self.contentPlaceholder.hidden = NO;
    }else{
        self.contentPlaceholder.hidden = YES;
    }

}

-(PubulicTextView*)titleTexFiled{
    if(!_titleTexFiled){
        _titleTexFiled=[[PubulicTextView alloc] initWithFrame:CGRectZero];
        _titleTexFiled.textView.font = BOLDSYSTEMFONT(18);
        _titleTexFiled.textView.textColor = KBlackColor;
       
    }
    return _titleTexFiled;
}
-(RZRichTextView*)contentTexFiled{
    if(!_contentTexFiled){
        _contentTexFiled=[[RZRichTextView alloc] initWithFrame:CGRectZero];
        _contentTexFiled.font = [UIFont systemFontOfSize:17];
        
        // 只针对当前textView的功能点击的实现
        _contentTexFiled.didClickedCell = ^BOOL(RZRichTextView * _Nonnull textView, RZRichTextAttributeItem * _Nonnull item) {
            if (item.type == RZRichTextAttributeTypeFontColor) {
                
                return YES;
            }
            return NO;
        };
        // 只针对当前textView 的工具条cell的实现
        _contentTexFiled.cellForItemAtIndePath = ^UICollectionViewCell * _Nonnull(UICollectionView * _Nonnull collectionView, NSIndexPath * _Nonnull indexPath, RZRichTextAttributeItem * _Nonnull item) {
            if (item.type == RZRichTextAttributeTypeFontBackgroundColor) {
               
            }
            return nil;
        };
        
    }
    return _contentTexFiled;
}
-(UILabel*)titlePlaceholder{
    if(!_titlePlaceholder){
        _titlePlaceholder=[UILabel new];
        _titlePlaceholder.textColor = UIColorFromRGB(0x959E97);
        _titlePlaceholder.font = SYSTEMFONT(14);
        _titlePlaceholder.text = @"请输入文章标题";
    }
    return _titlePlaceholder;
}
-(UILabel*)contentPlaceholder{
    if(!_contentPlaceholder){
        _contentPlaceholder=[UILabel new];
        _contentPlaceholder.textColor = UIColorFromRGB(0x959E97);
        _contentPlaceholder.font = SYSTEMFONT(14);
        _contentPlaceholder.text = @"正文";
    }
    return _contentPlaceholder;
}

-(NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray =[NSMutableArray array];
        [_imageArray addObject:@"https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=ad628627aacc7cd9e52d32d909032104/32fa828ba61ea8d3fcd2e9ce9e0a304e241f5803.jpg"];
        [_imageArray addObject:@"https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=a9e671b9a551f3dedcb2bf64a4eff0ec/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg"];
        [_imageArray addObject:@"https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=92afee66fd36afc3110c39658318eb85/908fa0ec08fa513db777cf78376d55fbb3fbd9b3.jpg"];
        [_imageArray addObject:@"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=a62e824376d98d1069d40a31113eb807/838ba61ea8d3fd1fc9c7b6853a4e251f94ca5f46.jpg"];
        [_imageArray addObject:@"https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=b5e4c905865494ee982209191df4e0e1/c2cec3fdfc03924590b2a9b58d94a4c27d1e2500.jpg"];
        [_imageArray addObject:@"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=0c78105b888ba61ec0eece2f713597cc/0e2442a7d933c8956c0e8eeadb1373f08202002a.jpg"];
        [_imageArray addObject:@"https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=2583e91fdb0735fa8ef048b9ae500f9f/060828381f30e9243602d73543086e061d95f714.jpg"];
        [_imageArray addObject:@"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=8b0a92229c13b07ea2bd56083cd69113/35a85edf8db1cb1339ac2910d254564e93584bef.jpg"];
        [_imageArray addObject:@"https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=b1db047f5e0fd9f9bf175369152dd42b/9a504fc2d5628535bffef9229fef76c6a7ef6392.jpg"];
        [_imageArray addObject:@"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=3d30051edb2a28345ca6300b6bb4c92e/e61190ef76c6a7ef9372bd25f2faaf51f3de6658.jpg"];
        [_imageArray addObject:@"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=f24de3fd36dbb6fd3a5be3263925aba6/8ad4b31c8701a18b24414549912f07082838fe0a.jpg"];
        [_imageArray addObject:@"https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=d5af9e3916d8bc3ed90800cab28ba6c8/e7cd7b899e510fb3b60a5c94d633c895d1430ca5.jpg"];
        [_imageArray addObject:@"https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=0211a1a7abc27d1eba263dc42bd5adaf/3812b31bb051f81950361049d5b44aed2e73e7a6.jpg"];
        [_imageArray addObject:@"https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=2ee3a9679beef01f52141ec5d0ff99e0/c2fdfc039245d6884e53a2a7abc27d1ed21b245b.jpg"];
        [_imageArray addObject:@"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=0ccda9d979094b36c4921ded93cd7c00/810a19d8bc3eb1350e9ae0bda91ea8d3fd1f4452.jpg"];

    }
    return _imageArray;
}

@end

@implementation ReleaseArticleCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = KRedColor;
    }
    return self;
}

@end
